package com.lichuandi.controller;

import com.lichuandi.listener.MyHttpSessionListener;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    /**
     * 负责新增一个session
     * @param request 请求
     * @return  字符串
     */
    @GetMapping("/addSession")
    public String addSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("name", "dalaoyang");
        return "当前在线人数" + MyHttpSessionListener.online;
    }

    /**
     * 负责销毁当前浏览器的Session。
     * @param request qq
     * @return -
     */
    @GetMapping("/removeSession")
    public String removeSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "当前在线人数" + MyHttpSessionListener.online;
    }

    /**
     * 查看在线人数。
     * @return --
     */
    @GetMapping("/online")
    public String online() {
        return "当前在线人数：" + MyHttpSessionListener.online + "人";
    }
}
